package com.serverapk.util;
import android.util.Base64;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Tool {
    /**
     * @param text      要签名的文本
     * @param secretKey 阿里云MQ secretKey
     * @return 加密后的字符串
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     */
    public static String macSignature(String text, String secretKey) throws InvalidKeyException, NoSuchAlgorithmException {
        Charset charset = Charset.forName("UTF-8");
        String algorithm = "HmacSHA1";
        Mac mac = Mac.getInstance(algorithm);
        mac.init(new SecretKeySpec(secretKey.getBytes(charset), algorithm));
        byte[] bytes = mac.doFinal(text.getBytes(charset));
        // android的base64编码注意换行符情况, 使用NO_WRAP
        String s = new String(Base64.encode(bytes, Base64.NO_WRAP), charset);
        return s;
    }
    
    
    public static String GetImageStr_Base64(String filePath)
    {//将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream in;;
        byte[] data = null;
        //读取图片字节数组
        try
        {
            in = new FileInputStream(filePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
       
        return new String(Base64.encode(data, Base64.NO_WRAP));
    }

}